﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using BLL;
using Model;
using Common;
using System.IO;
using System.Xml;
using AdminWeb.Filter;
using AdminWeb.Models;

namespace AdminWeb.Controllers
{
    [Area("Admin")]
    public class UEditorController : Controller
    {
        private static object ojb = new object();

        [CheckAdminLoginAjax]
        public string UploadFile()
        {
            string mess = "";
            string action = Request.Query["action"];
            switch (action)
            {
                case "config":
                    string filePath =Directory.GetCurrentDirectory()+"/wwwroot/Content/js/plugins/ueditor1_4_3/config.json";
                    var json = System.IO.File.ReadAllText(filePath);
                    return json;
                case "uploadimage":
                    Member admin = BaseOperater.getAdmin(HttpContext);
                    lock (ojb)
                    {
                        try
                        {
                            string savepath = Directory.GetCurrentDirectory()+ "/UploadFile/Blog";//图片保存路径
                            string url= "/UploadFile/Blog";//图片访问路径
                            var files = Request.Form.Files;
                            string uploadtype = Request.Query["uploadtype"];
                            // 单个文件上传
                            var file = files[0];
                            string type = file.ContentType;
                            if (type == "image/gif" || type == "image/jpg" || type == "image/jpeg" || type == "image/bmp" || type == "image/png")
                            {
                                string filename = file.FileName;//原名称
                                string filetype = filename.Split('.')[1];//类型
                                string newname = Utils.Number(10) + "." + filetype;//新名称
                                int filesize =Utils.ObjToInt(file.Length/1024,0);//大小（kb为单位）
                                string absolutePath = "";//绝对路径
                                string relativePath = "";//相对路径
                                string directory = "";//文件夹
                                switch (uploadtype)
                                {
                                    case "blog":
                                        //博客图片上传
                                        absolutePath = savepath+"/" + admin.mem_id + "/" + newname;
                                        relativePath = url+"/" + admin.mem_id + "/"+ newname;
                                        directory= savepath+"/"+ admin.mem_id;
                                        break;
                                }
                                //创建文件夹
                                if (!Directory.Exists(directory))
                                {
                                    Directory.CreateDirectory(directory);
                                }

                                using (FileStream fs = System.IO.File.Create(absolutePath))
                                {
                                    file.CopyTo(fs);
                                    fs.Flush();
                                }
                                #region 保存数据库

                                switch (uploadtype)
                                {
                                    case "blog":
                                        Blog_UploadBLL bubll = new Blog_UploadBLL();
                                        Blog_Upload model = new Blog_Upload();
                                        model.ul_id = Utils.Number(15);
                                        model.ul_size = filesize;
                                        model.ul_mimetype = type;
                                        model.ul_memid = admin.mem_id;
                                        model.ul_sourcename = filename;
                                        model.ul_name = newname;
                                        model.ul_addtime = DateTime.Now;
                                        bubll.Add(model);
                                        break;
                                }
                                
                                #endregion
                                mess = "{\"original\":\"" + filename + "\",\"name\":\"" + newname + "\",\"url\":\"" + relativePath + "\",\"size\":\"99697\",\"type\":\"" + filetype + "\",\"state\":\"SUCCESS\"}";
                            }
                        }
                        catch(Exception ex)
                        {
                            LogHelper.Error("上传文件失败", ex.ToString());
                            mess = "{\"state\":\"ERROR\"}";
                        }
                    }
                    break;
                case "uploadscrawl":

                    break;
                case "uploadvideo":

                    break;
                case "uploadfile":

                    break;
                case "listimage":

                    break;
                case "listfile":

                    break;
                case "catchimage":

                    break;
                default:

                    break;
            }
            return mess;
        }
    }
}